package edu.arizona.arid.akshen;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;

import edu.arizona.arid.akshen.util.Http;

public class MessageServiceThread extends Thread {

	private MessageServiceDelegate delegate;
	//private DataInputStream din;
	private DataInputStream in;

	public MessageServiceThread(MessageServiceDelegate delegate, InputStream in) {
		this.delegate = delegate;
		//this.din = (DataInputStream)in;
		this.in = new DataInputStream(in);
	}
	
	public void run() {
		while(true) {
			try {
				String response = Http.get("http://home.sumin.us/labs/recall/post.php");
				delegate.messageReceived(response);
			} catch (IOException e) {
				delegate.exceptionRaised(e);
				try {
					Thread.sleep(1000);
				} catch (InterruptedException ie) {
					delegate.exceptionRaised(ie);
				}
			}
		}
	}

	/*
	public void run() {
		try {
			byte[] buf = new byte[1024*4];
			//StringBuffer sbuf = new StringBuffer();
			delegate.messageReceived("Ready to read!");
			int read = 0;
			while((read = in.read(buf)) > 0) {
				delegate.messageReceived("Reading...");
				delegate.messageReceived(new String(buf, 0, read));
			}
			
		}
		catch (Exception e) {
			delegate.messageReceived(e.toString());
			delegate.exceptionRaised(e);
		}
		finally {
			try {
				delegate.messageReceived("Closing input stream");
				in.close();
			}
			catch (Exception e) {
				delegate.messageReceived(e.toString());
				delegate.exceptionRaised(e);
			}
		}
	}
	*/
}
